<template>
  <div>
    <el-dialog
      title="设置人员"
      :visible.sync="drawer"
      :close-on-click-modal="false"
      width="1200px"
      @close="CloseRoles()"
      @open="openhander"
    >
      <tranformcom
        :id="overallId"
        :loading="loading"
        :treedatalist="list"
        :checkedListNode="checkedListNode"
        :arrt="arrt"
        @savePersonHandle="savePersonHandle"
        ref="singleNode"
      ></tranformcom>
      <div class="btnbox">
        <el-button @click="drawer = false">关闭</el-button>
        <el-button type="primary" @click="SubmitRoles">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import tranformcom from '@/components/custom/singleNode.vue'
import { filterTreeHandeler } from '@/utils/tools/funtctionAPI.js'
import authorityApi from '@/utils/tools/authorityApi/index.js'
import { flatPidToTree } from '@/utils/tools/common'
export default {
  components: { tranformcom },
  data() {
    return {
      overallId: 'sysOrganizeUserId',
      RolesList: [],
      drawer: false,
      updateList: null,
      id: '',
      loading: false,
      arrt: {},
      submitList: [],
      list: [],
      checkedListNode: []
    }
  },
  created() {},
  mounted() {},
  methods: {
    SubmitRoles() {
      let list = this.submitList.map((e) => {
        return {
          realName: e.overallRealName,
          sysDepartmentId: e.overallDepartmentId,
          sysDepartmentName: e.overallDepartmentName,
          sysOrganizeId: e.overallOrganizeId,
          sysOrganizeName: e.overallOrganizeName,
          sysOrganizeUserId: e.sysOrganizeUserId,
          userId: e.overallUserID,
          userName: e.overallUserName
        }
      })
      if (!list.length) {
        return this.$message.error('未选择人员')
      }
      let id = this.id
      authorityApi.sysGroupPortUser.saveprocessUser({ groupId: id, userGroupInfo: list }, (res) => {
        this.$message.success(res)
        this.drawer = false
      })
    },
    CloseRoles() {
      this.RolesList = []
      this.list = []
      this.$refs.singleNode.clearhandler()
      this.submitList = []
    },
    savePersonHandle(list) {
      this.submitList = list
    },
    openhander() {
      this.loading = true
      let data = {
        queryType: 5
      }
      authorityApi.UserPort.getOrganizeAllUser(data, (res) => {
        this.loading = false
        this.list = flatPidToTree(res)
        filterTreeHandeler(this.list)
        this.checkedListNode.push(this.list[0].id)
        if (this.list && this.list.length > 0) {
          let id = this.id
          authorityApi.sysGroupPortUser.savesysGroupUserlist({ groupId: id }, (res) => {
            if (!res) {
              this.$refs.singleNode.checkedList = []
            } else {
              res.forEach((item) => {
                item.overallUserName = item.userName
                item.overallRealName = item.realName
                item.overallOrganizeName = item.sysOrganizeName
                item.overallDepartmentName = item.sysDepartmentName
              })
              this.$refs.singleNode.checkedList = JSON.parse(JSON.stringify(res))
              let list = res.map((item) => item.sysOrganizeUserId)
              this.$refs.singleNode.setTreeChecked(list)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.table {
  width: 100%;
  font-size: 12px;
}
/deep/.btnbox {
  margin-top: 20px;
  text-align: center;
  .el-button {
    font-size: 12px;
  }
}
</style>
